/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.control;

import java.util.Collection;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.colswe.testingProject.dao.DAO;
import org.colswe.testingProject.dao.PersonDAO;
import org.colswe.testingProject.entity.Person;

/**
 *
 * @author Andrés Esteban Paez Torres
 * @version 1.0
 */
public class PersonControl implements Control<Person> {

    private EntityManagerFactory emf;
    private DAO<Person> personDao;

    public PersonControl() {
        this.emf = Persistence.createEntityManagerFactory("TPPU");
        personDao = new PersonDAO(this.emf);
    }

    @Override
    public boolean create(Person toCreate) {
        return personDao.create(toCreate);
    }

    @Override
    public Person read(Person toRead) {
        return personDao.read(toRead);
    }

    @Override
    public boolean delete(Person toDelete) {
        return personDao.delete(toDelete);
    }

    @Override
    public boolean update(Person toUpdate) {
        return personDao.update(toUpdate);
    }

    @Override
    public Collection<Person> readAll() {
        return personDao.readAll();
    }

    public String userRole(Person person) {
        Control<Person> controlPerson = new PersonControl();
        Person other;
        other = controlPerson.read(person);
        if (other != null) {
            if (person.getPassword().equals(other.getPassword())) {
                return other.getRole();
            }
        }
        return null;
    }
}
